package com.hoe.tsb.fraft.serialize;

import com.hoe.tsb.fraft.event.msg.FRaftMessage;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 字节数组序列化
 * </p>
 *
 * @author hoe
 * @version 1.0
 * @date 2022/11/23
 */
public class SerializerHelper {

    private static final String DEFAULT_KEY = "default";
    private static final SerializerHelper instance = new SerializerHelper();
    private final Map<String, Serializer> serializerMap = new HashMap<>(2);

    private SerializerHelper() {
        serializerMap.put(DEFAULT_KEY, new BytesSerializer());
    }

    public static SerializerHelper getInstance() {
        return instance;
    }

    public static Serializer getSerializer(String key) {
        return getInstance().serializerMap.get(key);
    }

    public static Serializer getDefault() {
        return getInstance().serializerMap.get(DEFAULT_KEY);
    }

    public static byte[] ser(FRaftMessage t) {
        return getDefault().serialize(t);
    }

    public static FRaftMessage des(byte[] t) {
        return getDefault().deserialize(t);
    }

    public static byte[] serWithSign(FRaftMessage t) {
        return getDefault().serializeWithSign(t);
    }

    public static FRaftMessage desWithSign(byte[] t) {
        return getDefault().deserializeWithSign(t);
    }
    
}
